package netWorkPrograming.tcp.tcp02;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/27
 * 需求2：服务端接收多条消息，客户端发送多条消息
 */
public class ClientDemo {
    public static void main(String[] args) throws UnknownHostException {
        System.out.println("-------------客户端--------------");
        Socket socket = null;
        PrintWriter pw = null;
        Scanner sc=new Scanner(System.in);
        try{
            socket=new Socket(InetAddress.getLocalHost(),8888);
            while(true){
                System.out.println("请说：");
                String input=sc.nextLine();
                if(input.equals("exit")){
                    break;
                }
                pw=new PrintWriter(socket.getOutputStream(),true);//一定要刷新不然只能看见一句
                pw.println(input);
            }

        }catch (SocketException e){
            System.out.println("byebye");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(sc!=null){
                sc.close();
            }
            if(pw!=null){
                pw.close();
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
